#!/usr/bin/env python
# -*- coding: utf-8 -*-
# //返回需要展示的盈亏线
import time
from py_at.Enums import DirectType,OffsetType
import os

def dict2list(dic: dict):
    ''' 将字典转化为列表 '''
    keys = dic.keys()
    vals = dic.values()
    lst = [(key, val) for key, val in zip(keys, vals)]
    #self.DicInstrument = sorted(dict2list(self.DicInstrument), key=lambda x: x[0], reverse=True) 使用方法  转换为list
    return list


#---------------"检测是否是交易时段""""""""""""""""、
def PanDuanShiDuan(rmshiduan):
    """
    判断当前时间是否在交易时段
    :param rmshiduan: 交易策略的可交易时段dict
    :return: 如果在交易时段内  返回Ture  否则不返回
    """
    shijian=time.strftime("%H:%M:%S", time.localtime())
    for i in rmshiduan:
        if shijian >= i['Begin'] and shijian <= i['End']:
            return True

#--------------"""取整价格到合约最小价格变动"""
def roundToPriceTick( priceTick, price):
    """取整价格到合约最小价格变动"""
    if not priceTick:
        return price
    newPrice = round(price / priceTick, 0) * priceTick
    return newPrice



import  json
import numpy
##对对象进行转义的时候  有的时候碰见一些numpy或者一些系统没见过的数据类型就会报错 这种是转换numpy的 还有转换时间等等吧 暂时这么用
class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.integer):
            return int(obj)
        elif isinstance(obj, numpy.floating):
            return float(obj)
        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()
        else:
            return super(MyEncoder, self).default(obj)


#
#
# def getShowkDate(orders):
#     buyopen = []
#     sellclose = []
#     sellopen = []
#     buyclose = []
#     showline = []
#     #根据
#     for i in orders:
#         if i.Direction == DirectType.Buy and i.Offset == OffsetType.Open:  # 买开
#             buyopen.append(i)
#         if i.Direction == DirectType.Sell and i.Offset == OffsetType.Close:  # 卖平
#             sellclose.append(i)
#         if i.Direction == DirectType.Sell and i.Offset == OffsetType.Open:  # 卖开
#             sellopen.append(i)
#         if i.Direction == DirectType.Buy and i.Offset == OffsetType.Close:  # 买平
#             buyclose.append(i)
#     for index, item in enumerate(sellclose):
#         posiline = []
#         buyorder = buyopen[index]  # 先得到开仓的数据
#         buydict = {}
#         buydict['xAxis'] = buyorder.DateTime
#         buydict['yAxis'] = buyorder.Price
#         posiline.append(buydict)
#
#         buydict = {}
#         buydict['xAxis'] = item.DateTime
#         buydict['yAxis'] = item.Price
#         if item.Price - buyorder.Price >= 0:
#             buydict['lineStyle'] = {'normal': {'color': '#FD1050', 'width': 1}}
#         else:
#             buydict['lineStyle'] = {'normal': {'color': '#0CF49B', 'width': 1}}
#         posiline.append(buydict)
#         showline.append(posiline)
#
#     for index, item in enumerate(buyclose):
#         posiline = []
#         buyorder = sellopen[index]  # 先得到开仓的数据
#         buydict = {}
#         buydict['xAxis'] = buyorder.DateTime
#         buydict['yAxis'] = buyorder.Price
#         posiline.append(buydict)
#         buydict = {}
#         buydict['xAxis'] = item.DateTime
#         buydict['yAxis'] = item.Price
#         if item.Price - buyorder.Price <= 0:
#             buydict['lineStyle'] = {'normal': {'color': '#FD1050', 'width': 1}}
#         else:
#             buydict['lineStyle'] = {'normal': {'color': '#0CF49B', 'width': 1}}
#         posiline.append(buydict)
#         showline.append(posiline)
#     return showline
